package com.drinks.yiniang.io.api;

import com.alibaba.fastjson.JSON;
import com.drinks.yiniang.entity.common.StringJson;
import com.drinks.yiniang.entity.common.UpdateVersionJson;
import com.drinks.yiniang.entity.common.UploadImageJson;
import com.drinks.yiniang.io.http.HttpUrls;
import com.drinks.yiniang.io.http.ProgressRequestBody;
import com.hansen.library.entity.BaseJson;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.utils.LogUtils;

import java.io.File;

/**
 * Created by han on 2019/3/6 0006.
 */

public class CommonApiIO extends BaseApiIO {

    volatile static CommonApiIO instance;

    static public CommonApiIO getInstance() {
        if (instance == null) {
            synchronized (CommonApiIO.class) {
                if (instance == null) {
                    instance = new CommonApiIO();
                }
            }
        }
        return instance;
    }

    /**
     * 获取启动页广告
     *
     * @param callback
     */
    public void getLaunchAd(final APIRequestCallback<StringJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();

        MainApiIO.getInstance().postRequest(HttpUrls.API_LAUNCH_PAGE_AD, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("获取启动页广告===onSuccess");

                StringJson mDataJson = JSON.parseObject(res, StringJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 获取验证码
     *
     * @param mobile   登录手机号
     * @param callback
     */
    public void getSmsCode(String mobile, final APIRequestCallback<BaseJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();

        paramsMap.put("mobile", mobile);
        paramsMap.put("type", TYPE_LOGIN_TERMINAL);//类型 默认moes（签名一品脱） app（签名点酒）

        MainApiIO.getInstance().postRequest(HttpUrls.API_SEND_SMS, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("获取验证码===onSuccess");

                BaseJson mDataJson = JSON.parseObject(res, BaseJson.class);

                if (callback != null) {
                    if (mDataJson != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 验证手机验证码
     *
     * @param callback
     */
    public void doPhoneCodeVerify(String mobile, String code, final APIRequestCallback<StringJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();
        paramsMap.put("mobile", mobile);
        paramsMap.put("phone_code", code);

        MainApiIO.getInstance().postRequest(HttpUrls.API_VERIFY_PHONE_CODE, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("验证手机验证码===onSuccess");

                StringJson mDataJson = JSON.parseObject(res, StringJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 关于我们
     *
     * @param callback
     */
    public void getAboutUsInfo(final APIRequestCallback<String, Tuple2<Integer, String>> callback) {
        paramsMap.clear();

        MainApiIO.getInstance().postRequest(HttpUrls.API_ABOUT_US, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("关于我们===onSuccess");

                String mDataJson = JSON.parseObject(res, String.class);

                if (callback != null) {
                    if (mDataJson != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 获取app注册和支付协议
     *
     * @param type     类型 0默认 注册协议；1支付协议
     * @param callback
     */
    public void getRegisterAndPayAgreement(String type, final APIRequestCallback<StringJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();
        paramsMap.put("type", type);

        MainApiIO.getInstance().postRequest(HttpUrls.API_REGISTER_AND_PAY_AGREEMENT, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("获取app注册和支付协议===onSuccess");

                StringJson mDataJson = JSON.parseObject(res, StringJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }


    /************************************************上传图片***************************************/

    /**
     * 上传图片
     *
     * @param file             文件
     * @param progressListener 进度监听
     * @param callback
     */
    public void uploadImage(File file, ProgressRequestBody.ProgressListener progressListener, final APIRequestCallback<UploadImageJson, Tuple2<Integer, String>> callback) {
        uploadImage(1, file, progressListener, callback);
    }

    /**
     * 上传图片
     *
     * @param flag             标识
     * @param file             文件
     * @param progressListener 进度监听
     * @param callback
     */
    public void uploadImage(int flag, File file, ProgressRequestBody.ProgressListener progressListener, final APIRequestCallback<UploadImageJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();


        MainApiIO.getInstance().uploadImage(HttpUrls.API_UPLOAD_FILE, paramsMap, file, flag, progressListener, new APIRequestCallback<String, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(String res) {
                LogUtils.d("上传图片===onSuccess");

                UploadImageJson imgJson = JSON.parseObject(res, UploadImageJson.class);

                if (callback != null) {
                    if (imgJson != null) {
                        callback.onSuccess(imgJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**********************************************app检测更新**************************************/
    /**
     * 版本检测更新
     *
     * @param version  当前版本号
     * @param callback
     */
    public void doAppUpdate(int version, final APIRequestCallback<UpdateVersionJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();
        paramsMap.put("platform", "android");
        paramsMap.put("version", String.valueOf(version));

        MainApiIO.getInstance().postRequest(HttpUrls.API_VERSION_CHECK, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("版本检测更新===onSuccess");

                UpdateVersionJson mDataJson = JSON.parseObject(res, UpdateVersionJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null && mDataJson.getData().getVersion_info() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }
}
